package com.yi.uccn.controller;

import com.yi.uccn.model.Banner;
import com.yi.uccn.model.Sm;
import com.yi.uccn.service.BannerService;
import com.yi.uccn.utils.MessageResult;
import com.yi.uccn.utils.SmUtil;
import com.yi.uccn.utils.UploadUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Controller
@RequestMapping("/fastdfs")
public class FastDFSController {
    private static Logger logger = LoggerFactory.getLogger(FastDFSController.class);

    @Autowired
    BannerService bannerService;

    /**
     * banner图片文件上传
     * @param file 文件
     * @return
     */
    @RequestMapping("/banner")
    @ResponseBody
    public MessageResult banner(@RequestParam("file") MultipartFile file) {
        HashMap<Object, Object> map = new HashMap<>(16);
        MessageResult msgResult = MessageResult.ok();
        Banner banner = new Banner();

        if (file.isEmpty()) {
            return MessageResult.errorMsg("请选择要上传的文件！");
        }

        try {
//          //拿到源文件名称
			String oFileName=file.getOriginalFilename();
			//截取后缀 1.jpg
			String extName=
			oFileName.substring(oFileName.lastIndexOf("."));
			//正则表达式判断 判断不属于.jpg .png .gif
			if(!extName.matches(".(jpg|png|gif)$")){
				//说明后缀非法
				 msgResult = MessageResult.errorMsg("文件后缀名非法！");
				return msgResult;
			}
			
			//使用file创建多级目录 d://img/upload/3/d/3//3/d3
			String path="D:\\Workspace\\yvning\\src\\main\\resources\\static\\images\\";
			File _dir=new File(path);
			//判断路径文件夹结构是否已经存在,不存在创建
			if(!_dir.exists()){//需要创建多级
				_dir.mkdirs();
			}
			//重命名文件 
			//dd135a52-3361-4a4f-ad68-79e5826152ab.jpg
			String newFileName=oFileName;
			//将图片流输出到 path+newFileName
			file.transferTo(new File(path+newFileName));
			//生成一个可访问图片的url地址
			//http://image.jt.com/easymall/c/4/b/7/b/5/8/f/5adda796-66af-4c6e-a9e5-49a52a3c44a5_371cee6d-d81b-42b7-a11f-3ad36dc0e537.jpg
			String url="/images/"+oFileName;
			Banner record=new Banner();
			record.setImgOptions(url);
			record.setStatus((byte)0);
			record.setCreateTime(new Date());
			bannerService.saveSelective(record);
			map.put("data", record);
			
        } catch (Exception e) {
            logger.error("上传失败！", e);
            msgResult =MessageResult.errorMsg("上传失败！" + e.getMessage());
        }
        msgResult.setData(map);

        return msgResult;
    }

    /**
     * 文章图片上传
     * @param file 文件
     * @return
     */
    @RequestMapping("/article")
    @ResponseBody
    public MessageResult article(@RequestParam("file") MultipartFile file) {
    	HashMap<Object, Object> map = new HashMap<>(16);
        MessageResult msgResult = MessageResult.ok();
        Banner banner = new Banner();

        if (file.isEmpty()) {
            return MessageResult.errorMsg("请选择要上传的文件！");
        }

        try {
//          //拿到源文件名称
			String oFileName=file.getOriginalFilename();
			//截取后缀 1.jpg
			String extName=
			oFileName.substring(oFileName.lastIndexOf("."));
			//正则表达式判断 判断不属于.jpg .png .gif
			if(!extName.matches(".(jpg|png|gif)$")){
				//说明后缀非法
				 msgResult = MessageResult.errorMsg("文件后缀名非法！");
				return msgResult;
			}
			
			//使用file创建多级目录 d://img/upload/3/d/3//3/d3
			String path="D:\\Workspace\\yvning\\src\\main\\resources\\static\\images\\";
			File _dir=new File(path);
			//判断路径文件夹结构是否已经存在,不存在创建
			if(!_dir.exists()){//需要创建多级
				_dir.mkdirs();
			}
			//重命名文件 
			//dd135a52-3361-4a4f-ad68-79e5826152ab.jpg
			String newFileName=oFileName;
			//将图片流输出到 path+newFileName
			file.transferTo(new File(path+newFileName));
			//生成一个可访问图片的url地址
			//http://image.jt.com/easymall/c/4/b/7/b/5/8/f/5adda796-66af-4c6e-a9e5-49a52a3c44a5_371cee6d-d81b-42b7-a11f-3ad36dc0e537.jpg
			String url="/images/"+oFileName;
			Banner record=new Banner();
			record.setImgOptions(url);
			record.setStatus((byte)0);
			record.setCreateTime(new Date());
			map.put("data", record);
			
        } catch (Exception e) {
            logger.error("上传失败！", e);
            msgResult =MessageResult.errorMsg("上传失败！" + e.getMessage());
        }
        msgResult.setData(map);

        return msgResult;
    }
}
